// pages/order/order.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabState: '',
    tab1: {
      list: [{
        id: 'tomeal',
        name: '待取餐'
      }, {
        id: 'topay',
        name: '已完成'
      }, {
        id: 'remove',
        name: '代付款'
      }, {
        id: 'pay',
        name: '已支付'
      }, {
        id: 'meal',
        name: '已取餐'
      }],
      selectedId: 'pay'
    },
    list: [], //订单列表
    imgUrl: '', //图片地址
    deleteStatus: false, //删除状态
    hidden: 'false',
    typesNum: 5,
    status: '',
    logfee: "",
    textareaInput: '',
    order: '',
    orderTitle: '',
    totalprice: '',
    realMoney: '',
    addressid: '',
    consumeMoney: ''
  },

  /**
   * tab点击触发事件
   */
  changeMeal: function(e) {
    console.log(e)
    let val;
    let _this = this
    if (e == undefined) {
      val = 'tomeal';
    } else {
      val = e.detail
    }
    this.setData({
      tabState: val
    })
    switch (val) {
      case "tomeal":
        _this.setData({
          deleteStatus: true
        })
        _this.getOrderData(7)
        break;
      case "topay":
        _this.setData({
          deleteStatus: false
        })
        _this.getOrderData(10)
        break;
      case "remove":
        _this.setData({
          deleteStatus: false
        })
        _this.getOrderData(6)
        break;
      case "pay":
        _this.setData({
          deleteStatus: true
        })
        _this.getOrderData(5)
        break;
      case "meal":
        _this.setData({
          deleteStatus: false
        })
        _this.getOrderData(8)
        break;
    }
  },

  /**
   * 获得数据
   */
  getOrderData: function(status) {
    var userid = app.globalDataO.openid.objectSid
    let _this = this
    app.gets(app.url.order.select + '?wechatId=' + userid + '&status=' + status).then(re => {
      console.log(re.data)
      _this.setData({
        imgUrl: re.data.urls,
        list: re.data.billHisLine,
        status: re.data.status,
        hidden: 'true',
      })
    })
  },
  //打电话
  phone(e) {
    var phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
    })

  },
  //评论
  comment_tab(e) {
    var billid = e.currentTarget.dataset.billid
    var resid = e.currentTarget.dataset.resid
    console.log(billid, resid)
    wx.navigateTo({
      url: '../../pages/comment/comment?billid=' + billid + '&resid=' + resid,
    })
  },
  /**
   * 点击查看详情
   */
  PaymentCompletion: function(e) {
    let tab = this.data.tabState;
    console.log(e.currentTarget.dataset.id)
    if (tab == 'topay') {
      wx.navigateTo({
        url: "../not_order_details/details?status=" + true + '&id=' + e.currentTarget.dataset.id
      })
    } else if (tab == 'meal') {
      wx.navigateTo({
        url: "../end_order_details/details?id=" + e.currentTarget.dataset.id
      })
    } else {
      wx.navigateTo({
        url: "../not_order_details/details?status=" + false + '&id=' + e.currentTarget.dataset.id
      })
    }
  },
  /**
   * 删除商品
   */
  _deleteData: function(e) {
    var status = this.data.status
    let _this = this
    console.log(status)
    wx.showModal({
      title: '确定删除吗',
      success(res) {
        if (res.confirm) {
          console.log(e.currentTarget.dataset.id)
          app.gets(app.url.order.delete + "?billHisId=" + e.currentTarget.dataset.id + '&status=' + status).then(re => {
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            })
            _this.getOrderData(status)
          })
          _this.getOrderData(status)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var status = options.status
    var name = options.name
    this.setData({
      ["tab1.selectedId"]: name
    })

    this.getOrderData(status)
    this.changeMeal();
  },
  wxPay(e) {
    var that = this
    console.log(e.currentTarget.dataset.id)
    app.post(app.url.order.cash, {
      billHisId: e.currentTarget.dataset.id
    }).then(res => {
      console.log(res)
      var addressid = res.data.addressId
      var consumeMoney = res.data.consumeMoney
      var logfee = res.data.logfee
      var order = res.data.payOrderId
      var orderTitle = res.data.payOrderTitle
      var realMoney = res.data.realMoney
      var textareaInput = res.data.decsription
      this.pay(addressid, consumeMoney, logfee, order, orderTitle, realMoney, textareaInput) 
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  //支付
  pay(addressid, consumeMoney, logfee, order, orderTitle, realMoney, textareaInput, ) {
    let openid = app.globalDataO.openid.openid
    app.post(app.url.wxPay1, {
      openid: openid,
      payOrderTitle: orderTitle,
      payOrderId: order,
      realMoney: realMoney,
      logfee: logfee,
      addressId: addressid,
      consumeMoney: consumeMoney,
      decsription: textareaInput
    }).then(res => {
      console.log(res)
      let nonceStr = res.data.data.nonceStr
      let packages = res.data.data.package
      let paysign = res.data.data.paySign
      let timeStamp = res.data.data.timeStamp
      let signtype = 'MD5'
      wx.requestPayment({
        timeStamp: timeStamp,
        nonceStr: nonceStr,
        paySign: paysign,
        signType: signtype,
        package: packages,
        success(res) {
          console.log(res)
          wx.showToast({
            title: '支付成功',
          })
          wx.navigateTo({
            url: '../../pages/orderlist/orderlist?status=5' + '&name=pay',
          })
        },
        fail() {
          wx.showToast({
            image: '../../images/gantan.png',
            title: '支付失败',
          })

        }
      })
    })

  },
  /**
   * 
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
})